<?php
function advert_block_info() 
{
  $blocks['hello-message'] = array('info' => t('Message'));
  return $blocks;
}
function advert_block_view($delta = '') {
  $block = array();
 
  if ($delta == 'hello-message') {
    $block['subject'] = t('Message');
    $block['content'] = t('Hello World!');
  }
 
  return $block;
}
/**
*Вспомогаьельный модуль - для новостей справа
*/
function advert_news_widget()
{
$articles=db_query("SELECT * FROM node WHERE type='newarticle' AND status=1 AND promote=1 ORDER BY created DESC LIMIT 5");
$out='<h3>Новости и статьи</h3>';
foreach($articles as $article)
  {
   
    $out.='<div class="one_news">';
  
	$out.='<div class="newstitle">'.l($article->title,'node/'.$article->nid).'</div>
	<div class="newsdate">'.date('d.m.Y',$article->created).'</div>
</div>';
  }
 $out.='<div class="morelink">'.l('Архив','news').'</div>';
return $out;
}

function advert_news_archive($yyyymm)
{
global $conf;
$count=10;
if(!empty($yyyymm))
{
$title='Архив '.$conf['month_namez'][(int)substr($yyyymm,4)-1].' '.substr($yyyymm,0,4);
drupal_set_title($title);
}else{
  
  $title=drupal_get_title();
  drupal_set_title($title);
}
  $breadcrumb[] = l(t('Home'), NULL);
  $breadcrumb = 'Новости';
  drupal_set_breadcrumb($breadcrumb);
  //drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name);

  // Set the term path as the canonical URL to prevent duplicate content.
 # $uri = entity_uri('taxonomy_term', $term);
#  drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE);
  // Set the non-aliased path as a default shortlink.
  #drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE);

  $build['term_heading'] = array(
    '#prefix' => '<div class="term-listing-heading">',
    '#suffix' => '</div>',
#    'term' => taxonomy_term_view($term, 'full'),
  );
  $query = db_select('node','n');
 $query->join('field_data_body','frb','frb.entity_id=n.nid');
 $query->condition('n.type','newarticle')->condition('n.status',1);
if(!empty($yyyymm))$query->where("FROM_UNIXTIME(n.created,'%Y%m')=:date",array(':date' => $yyyymm));
  $count_query = clone $query;
$count_query->addExpression('COUNT(n.nid)');
 $query->fields('n',array('nid'))->fields('frb',array('body_value'));
 $query = $query->extend('PagerDefault');
 $query = $query->limit($count);
  $query->setCountQuery($count_query);
 $query->orderBy('n.created','DESC');

 $nid_rec=$query->execute();

while($nid_=$nid_rec->fetchObject())
{
$nodes[]=node_load(array('nid'=>$nid_->nid));

#$nids[]=$nid_->nid;
}
  if (count($nodes)) {
  
  $build['pager'] = array(
#    '#prefix'=>'<div style="position:relative;">',
    #'#suffix'=>'</div>',
      '#theme' => 'pager',
      '#weight' => 7,
    );
  
   /* des = node_load_multiple($nids);    
$build += node_view_multiple($nodes);*/
$nodes_output='<h1 class="title">'.$title.'</h2>';
  foreach($nodes as $node)
    {
   #pre($node);
     
  #echo $node->field_img['und'][0]['uri']."<hr>";
    $nodes_output .= '<div class="one_news">';
          $nodes_output .= '<div class="thumb" style="float:left;margin:0 5px 0 0">';
          if(!empty($node->field_img['und'][0]['uri']))$nodes_output .= l('<img src="'.image_style_url('200_200',$node->field_img['und'][0]['uri']).'">','node/'.$node->nid,array('html'=>true));
          $nodes_output .= '</div>';
	$nodes_output .= '<div class="newstitle">'.l($node->title,'node/'.$node->nid).'</div>
	<div class="newsdate">'.date('d.m.Y',$node->created).'</div>
	<div class="newstext">'.mb_substr(strip_tags($node->body_value,'<a><br><font><b>'),0,250).'...</div>
	</div>';

    }
  $build['nodes']['#sorted'] = TRUE;
    $build['nodes']['#prefix']='<div>'.$nodes_output;
        
    #$build['nodes']['#prefix']='<div class="advert_taxonomy">';
    $build['nodes']['#suffix']='</div>';
    $build['nodes']['#weight']=6;
    #pre($build,1);
    #$build['box1']=array('#type'=>'html','#prefix'=>'</div>');
  }
  else {
    $build['no_content'] = array(
      '#prefix' => '<p>',
      '#markup' => t('There is currently no content classified with this term.'),
      '#suffix' => '</p>',
    );
  }
  return $build;

}
function _advert_comp_news_archive_block()
{
global $conf;
#$conf['month_namez']
$dd=db_query("SELECT FROM_UNIXTIME(created,'%Y%m') as date FROM node WHERE type='newarticle' AND status=1 GROUP BY FROM_UNIXTIME(created,'%Y%m') ORDER BY FROM_UNIXTIME(created,'%Y%m') DESC LIMIT 12");
$out='<h3>Архив новостей</h3>
<ul class="newsarchive_block">';
foreach($dd as $d)
	{
	$day=(int)substr($d->date,4);
	$year=substr($d->date,0,4);
	$out.='<li>'.l($conf['month_namez'][$day-1],'news/'.$d->date).'</li>';
	}
$out.='</ul>';	
return $out;
}
function _advert_comp_news_link($nid)
{
$query=db_select('node','n');
$query->join('field_data_field_id_event','fafie','fafie.entity_id=n.nid');
$query->condition('n.type','newarticle')->condition('n.status',1)->condition('fafie.field_id_event_value',$nid);
$query->fields('n',array('nid','title'));
$newnode=$query->execute()->fetchObject(); 
if($newnode->nid)$out=l($newnode->title,'node/'.$newnode->nid);
  return $out;
# db_select
}
?>